package com.sunyard.pcc.common.exception;

import com.sunyard.pcc.common.constant.ErrorCodeEnum;
import com.sunyard.michiru.common.entity.BaseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author: Hewei
 * @Description:
 * @Date: Created in 15:22 2018/2/26
 */
@RestControllerAdvice
@Slf4j
public class GlobalControllerExceptionHandler {

    @ExceptionHandler(value = {PccCommonException.class})
    @ResponseStatus(value = HttpStatus.OK)
    public BaseResult pccCommonException(PccCommonException e){
        log.error("捕获到一个PccCommonException:{}", e);
        return BaseResult.create(e.getCode(), e.getMessage());
    }



    @ExceptionHandler(value = {RuntimeException.class})
    @ResponseStatus(value = HttpStatus.OK)
    public BaseResult runtimeException(RuntimeException e){
        log.error("捕获到一个RuntimeException:{}", e);
        return BaseResult.create(ErrorCodeEnum.SYS_ERROR.getErrorCode(), ErrorCodeEnum.SYS_ERROR.getErrorMsg());
    }


    @ExceptionHandler(value = {Exception.class})
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public BaseResult noHandlerException(Exception e) {
        log.error("获取到一个Exception异常:{}", e);
        return BaseResult.create(ErrorCodeEnum.SYS_ERROR.getErrorCode(), ErrorCodeEnum.SYS_ERROR.getErrorMsg());
    }

}
